import requests
import json
import re
import pandas as pd
from datetime import datetime

num_code1 = {
    '&#xeba2;': '0',
    '&#xed8f;': '1',
    '&#xe886;': '2',
    '&#xe8d7;': '3',
    '&#xf16b;': '4',
    '&#xe83d;': '5',
    '&#xf23f;': '6',
    '&#xe132;': '7',
    '&#xf05a;': '8',
    '&#xec4b;': '9',
}

num_code2 = {
    '&#xf7b3;': '0',
    '&#xedba;': '1',
    '&#xf0f0;': '2',
    '&#xe85f;': '3',
    '&#xefe9;': '4',
    '&#xed4f;': '5',
    '&#xf70e;': '6',
    '&#xe916;': '7',
    '&#xe83f;': '8',
    '&#xed98;': '9',
}

num_code3 = {
    '&#xf726;': '0',
    '&#xf85e;': '1',
    '&#xe99c;': '2',
    '&#xe8d7;': '3',
    '&#xe8ee;': '4',
    '&#xecdc;': '5',
    '&#xf7ff;': '6',
    '&#xeb92;': '7',
    '&#xf1fc;': '8',
    '&#xe9ea;': '9',
}

num_code4 = {
    '&#xf66d;': '0',
    '&#xe6d5;': '1',
    '&#xec68;': '2',
    '&#xf615;': '3',
    '&#xeab3;': '4',
    '&#xef74;': '5',
    '&#xe5ac;': '6',
    '&#xe317;': '7',
    '&#xe1b7;': '8',
    '&#xe274;': '9',
}

num_code5 = {
    '&#xea6f;': '0',
    '&#xed30;': '1',
    '&#xe3ec;': '2',
    '&#xf11c;': '3',
    '&#xf7d2;': '4',
    '&#xf3e8;': '5',
    '&#xea60;': '6',
    '&#xe3df;': '7',
    '&#xef28;': '8',
    '&#xeb19;': '9',
}

code_list = [num_code1, num_code2, num_code3, num_code4, num_code5]


def recode_codes(movie_piao_fang):
    for code_dict in code_list:
        kvwords = code_dict.items()
        for k, v in kvwords:
            movie_piao_fang = movie_piao_fang.replace(k, v)

    return movie_piao_fang


if __name__ == '__main__':
    url1 = 'https://piaofang.maoyan.com/dashboard-ajax?orderType=0&uuid=1938b6042b7c8-095c6f731f042e-26011851-1fa400-1938b6042b7c8&timeStamp=1733215970935&User-Agent=TW96aWxsYS81LjAgKFdpbmRvd3MgTlQgMTAuMDsgV2luNjQ7IHg2NCkgQXBwbGVXZWJLaXQvNTM3LjM2IChLSFRNTCwgbGlrZSBHZWNrbykgQ2hyb21lLzEzMS4wLjAuMCBTYWZhcmkvNTM3LjM2&index=713&channelId=40009&sVersion=2&signKey=b9ba8a808c9acc49342231412025a24b'

    headers = {
        'cookie': '_lxsdk_cuid=1938b6042b7c8-095c6f731f042e-26011851-1fa400-1938b6042b7c8; _lxsdk=1938b6042b7c8-095c6f731f042e-26011851-1fa400-1938b6042b7c8; _lx_utm=utm_source%3DBaidu%26utm_medium%3Dorganic; _lxsdk_s=1938bb8371a-307-90-43f%7C%7C2',
        'host': 'piaofang.maoyan.com',
        'referer': 'https://piaofang.maoyan.com/dashboard',
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36'
    }

    response = requests.get(url=url1, headers=headers)

    time_now = datetime.strftime(datetime.now(),'%Y-%m-%d %H-%M-%S')
    # print(response.status_code)
    response.encoding = 'utf-8'
    info = response.text
    print(info)

    # 将上述爬取到的json格式的字符串转成python中的字典进行数据分析
    info = json.loads(info)

    movie_id_list = []
    movie_name_list = []
    movie_days_list = []
    movie_box_list = []
    splitBoxRate_list = []
    # print(info, type(info))
    movies_list = info["movieList"]["data"]["list"]
    for movie in movies_list:
        # 电影名称
        movie_id = movie['movieInfo']['movieId']
        movie_id_list.append(movie_id)

        movie_name = movie['movieInfo']['movieName']
        movie_name_list.append(movie_name)
        movie_days = movie['movieInfo']['releaseInfo']
        movie_days_list.append(movie_days)
        movie_box = str(movie['boxSplitUnit']['num'])
        movie_box = recode_codes(movie_box)
        movie_box_list.append(movie_box)
        splitBoxRate = movie['splitBoxRate']
        splitBoxRate_list.append(splitBoxRate)
        print(
            f"电影编号：{movie_id}, 电影名称：{movie_name}，上映天数：{movie_days}，综合票房：{movie_box}，票房占比：{splitBoxRate}")
        print("----------------------")

    movie_dict = {
        '电影编号': movie_id_list,
        '电影名称': movie_name_list,
        '上映天数': movie_days_list,
        '综合票房': movie_box_list,
        '票房占比': splitBoxRate_list
    }

    df1 = pd.DataFrame(movie_dict)

    df1.to_csv(f'data/猫眼综合票房数据-{time_now}.csv', index=False)


